#!/usr/bin/env bash
set -Eeuo pipefail

cd $(dirname $(readlink -f $0))/src
rm -f main.sh *.log 

APP="OHEMU"
SUPPORT="https://github.com/SSRVodka/oh-qemu-docker"
PROCESS="${APP,,}"
PROCESS="${PROCESS// /-}"

RAM_SIZE=4096M
INSTANCE_NUM=2
STORAGE=/tmp/storage

RUN_LOCAL=1

read -p "❯ Warning: Network on the host will be modified if you run this script locally. Still want to continue? (Y/[n]) " cont
if [ "$cont" != "Y" ]; then
    echo "Abort."
    exit 0
fi

. init.sh
# network helpers
. network.sh

init ${RAM_SIZE} ${STORAGE} "Y" ${INSTANCE_NUM} 1 # RAM check

info "Initializing network..."
initializeDNS
_getNetworkInfo ""  # Get IP, Gateway, Interface(DEV)

info "Host: $HOST  IP: $IP  Gateway: $GATEWAY  Interface: $VM_NET_DEV"
    [ -f /etc/resolv.conf ] && grep '^nameserver*' /etc/resolv.conf
echo

# Global count
GLOBAL_CNT=0

main() {

    # For different sound card
    OTHER_PARAMS=$1

    # param list index
    CNT=0
    read -r -a IMG_DIR_LIST <<< "${OHOS_IMG_DIR_LIST}"
    read -r -a MAC_LIST <<< "${INSTANCE_MAC_LIST}"
    for sn in $INSTANCE_SN_LIST
    do
        # No parameter: random MAC address
        _getNetworkInfo ${MAC_LIST[$CNT]}   # Get random MAC
        # NET_OPTS="-netdev bridge,id=net0,br=${BRIDGE_NAME} -device virtio-net-pci,netdev=net0,mac=${VM_NET_MAC}"
        _configureNAT $GLOBAL_CNT $VM_NET_MAC      # Get NET_OPTS
        KERNEL_PARAMS="\
        ip=dhcp \
        loglevel=7 \
        console=ttyS${GLOBAL_CNT},115200 \
        init=init root=/dev/ram0 \
        rw \
        ohos.boot.hardware=virt \
        default_boot_device=10007000.virtio_mmio \
        sn=${sn} \
        ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required \
        ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required \
        "
        KERNEL_PARAMS=$(echo "$KERNEL_PARAMS" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -s ' ')
        OHOS_IMG_DIR=${IMG_DIR_LIST[$CNT]}
        ARGS="\
        -machine pc \
        -smp ${SMP} \
        -m $RAM_SIZE \
        -cpu host \
        -boot c \
        -enable-kvm \
        -nographic \
        -vga virtio \
        -display vnc=:${GLOBAL_CNT} \
        -rtc base=utc,clock=host \
        ${OTHER_PARAMS} \
        -initrd ${OHOS_IMG_DIR}/ramdisk.img \
        -kernel ${OHOS_IMG_DIR}/bzImage \
        ${NET_OPTS} \
        -drive file=${OHOS_IMG_DIR}/updater.img,if=virtio,media=disk,format=raw,index=0 \
        -drive file=${OHOS_IMG_DIR}/system.img,if=virtio,media=disk,format=raw,index=1 \
        -drive file=${OHOS_IMG_DIR}/vendor.img,if=virtio,media=disk,format=raw,index=2 \
        -drive file=${OHOS_IMG_DIR}/sys_prod.img,if=virtio,media=disk,format=raw,index=3 \
        -drive file=${OHOS_IMG_DIR}/chip_prod.img,if=virtio,media=disk,format=raw,index=4 \
        -drive file=${OHOS_IMG_DIR}/userdata.img,if=virtio,media=disk,format=raw,index=5 \
        -append \"${KERNEL_PARAMS}\" \
        "
        info "Instance $GLOBAL_CNT arguments:"
        echo $ARGS
        ARGS=$(echo "$ARGS" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -s ' ')
        CNT=$(($CNT+1))
        GLOBAL_CNT=$(($GLOBAL_CNT+1))
        echo "nohup qemu-system-x86_64 ${ARGS} 2>&1 > ./ohemu-$GLOBAL_CNT.log &" >> main.sh
    done

    . main.sh
}

SMP=6 OHOS_IMG_DIR_LIST=./out/x86_64_virt/packages/phone/images INSTANCE_SN_LIST=8823456788 INSTANCE_MAC_LIST=02:CB:4D:2B:B1:A5
main "-audiodev alsa,id=au1,out.dev=default \
-device es1370,id=sound0,bus=pci.0,audiodev=au1"
SMP=6 OHOS_IMG_DIR_LIST=./out2/x86_64_virt/packages/phone/images INSTANCE_SN_LIST=8823456789 INSTANCE_MAC_LIST=02:CB:4D:2B:B1:A6
main "-audiodev pa,id=snd0 -device AC97,audiodev=snd0"
